#import <Foundation/Foundation.h>
struct MyIntegerFloatStruct {
  int i;
  float f;
};
int main() {

  @autoreleasepool {
    /// NSValue
    NSString *mainString = @"This is a short string";
    // NSRange substringRange = [mainString rangeOfString:@"long"];
    NSRange substringRange = [mainString rangeOfString:@"short"];
    NSValue *rangeValue = [NSValue valueWithRange: substringRange];

    struct MyIntegerFloatStruct structObj;
    structObj.i = 10;
    structObj.f = 3.14f;
    // must use 'struct' tag to refer to type 'MyIntegerFloatStruct'
    NSValue *structValue = [NSValue value:&structObj withObjCType: @encode(struct MyIntegerFloatStruct)];


    /// NSArray
    // obj-c中异质容器很灵活
    NSArray *array1 = [NSArray arrayWithObjects: rangeValue, structValue, [NSNull null], @"str", nil];

    NSArray *array2 = @[@"a", @"hello", @"dyldw"];

    NSUInteger numberOfItems = [array1 count];
    if(numberOfItems > 0) {
      NSLog(@"array1 size = %lu", numberOfItems);
      NSLog(@"first item is: %@", [array1 objectAtIndex:0]);
      // cannot pass object with interface type 'NSArray' by value to variadic function; expected type from format string was 'id'
      // NSLog(@"first item is: %@", array1[0]);
    }
    if([array1 containsObject:rangeValue]) {
      NSLog(@"exist range value");
    }

    NSArray *unsortedStrings = @[@"gammaString", @"alphaString", @"betaString"];
    NSArray *sortedStrings = [unsortedStrings sortedArrayUsingSelector:@selector(compare:)];
    
    NSMutableArray *mutableArray = [NSMutableArray array];
    [mutableArray addObject:@"gamma"];
    [mutableArray addObject:@"alpha"];
    [mutableArray addObject:@"beta"];
    [mutableArray replaceObjectAtIndex:0 withObject:@"epsilon"];
    [mutableArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

    NSString* path = @"/home/yilongdong/Programming-with-Objective-C-in-Chinese-dev/practice/7值与集合类型/archivedFile.data";
    NSURL *url=[NSURL fileURLWithPath:path];
    BOOL success = [mutableArray writeToURL:url atomically:YES];
    if(success == NO) {
      NSLog(@"Serialization failure");
    } else {
      NSLog(@"Serialization successful");
    }

    NSArray* immutableArray = [NSArray arrayWithContentsOfFile: path];
    if(!immutableArray) {
      NSLog(@"Deserialization failure");
    }
    else {
      NSLog(@"Deserialization successful");
    }

    for(id obj in immutableArray) {
      NSLog(@"Object: %@", obj);
    }

    for(id obj in [immutableArray reverseObjectEnumerator]) {
      NSLog(@"Object: %@", obj);
    }


    /// NSSet
    NSSet* numberSet = [NSSet setWithObjects: @1, @2, @3, nil];

    /// NSDictionary
    NSDictionary *kv = @{
      @"a" : @"d",
      @"b" : @"y",
      @"c" : @"l",
      @"d" : @1
    };

    for(NSString* key in kv) {
      NSLog(@"key = %@, value = %@", key, [kv objectForKey: key]);
    }
  }
  return 0;
}